package com.itheima.hmshopping.app.controller;

import com.itheima.hmshopping.app.service.SmsFlashPromotionProductRelationService;
import com.itheima.hmshopping.entity.SmsFlashPromotion;
import com.itheima.hmshopping.entity.SmsFlashPromotionProductRelation;
import com.itheima.hmshopping.util.HmPage;
import com.itheima.hmshopping.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("flashProductRelation")
public class SmsFlashPromotionProductRelationController {
    @Autowired
    private SmsFlashPromotionProductRelationService productRelationService;

    /**
     * 批量选择商品添加关联
     * @param list
     * @return
     */
    @PostMapping("create")
    public R create(@RequestBody List<SmsFlashPromotionProductRelation> list) {
        productRelationService.create(list);
      return R.succeed("ok","");

    }

    /**
     * 删除关联
     * @param id
     * @return
     */
    @PostMapping("delete/{id}")
    public R delete(@PathVariable Integer id){

        productRelationService.delete(id);
        return R.succeed("ok","");
    }

    /**
     * 分页查询不同场次关联及商品信息
     * @param flashPromotionId
     * @param flashPromotionSessionId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("list")
    public R list(Integer flashPromotionId,Integer flashPromotionSessionId,@RequestParam(defaultValue = "1") Integer pageNum,
                  @RequestParam(defaultValue = "10") Integer pageSize ){
        HmPage hmPage=productRelationService.list(flashPromotionId,flashPromotionSessionId,pageNum,pageSize);
        return R.succeed(hmPage,"");

    }

    /**
     * 修改关联信息
     * @param id
     * @param relation
     * @return
     */
    @PostMapping("update/{id}")
    public R update(@PathVariable Long id,@RequestBody SmsFlashPromotionProductRelation relation){
        productRelationService.update(id,relation);
        return R.succeed("ok","");
    }

    /**
     * 获取场次详情
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public R getOne(@PathVariable Long id){
     SmsFlashPromotionProductRelation relation= productRelationService .getOne(id);
     return R.succeed(relation,"ok");
    }
    }


